package com.java.wmovie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.wmovie.entity.User;
import com.java.wmovie.mapper.UserMapper;
import com.java.wmovie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class loginController {
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/login")
    public String login(){
        return "/login";
    }

    @RequestMapping("/logins")
    public String logins(Model model, @RequestParam("uName")String uName, @RequestParam("uPassword")String uPassword, HttpSession session){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        User user=userMapper.selectOne(new QueryWrapper<User>().eq("u_name",uName).eq("u_password",uPassword));
        if(ObjectUtils.isEmpty(user)){
            model.addAttribute("msg","账号或密码错误");
            session.setAttribute("loginUser",null);
            return "/login";
        }else {
            session.setAttribute("loginUser",uName);
            return "redirect:/";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session,Model model){
        String name= (String) session.getAttribute("loginUser");
        model.addAttribute("name",name);
        session.setAttribute("loginUser",null);
        return "redirect:/";
    }

}
